package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: liudehuan
 * @description
 * @date: 2023-04-25 1:32
 */
@Slf4j
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {


    /**
     * 管理端获取敏感词列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult getSensitives(SensitiveDto dto) {
        LambdaQueryWrapper<WmSensitive> lbw = new LambdaQueryWrapper<>();
        if(null != dto.getName()){
            lbw.like(WmSensitive::getSensitives,dto.getName());
        }

        lbw.orderByDesc(WmSensitive::getCreatedTime);
        Page<WmSensitive> dtoPage = new Page<>(dto.getPage(),dto.getSize());
        Page<WmSensitive> dtos = this.page(dtoPage, lbw);
        ResponseResult result = new ResponseResult();
        result.setCode(200);
        result.setData(dtos.getRecords());
        result.setTotal((int) dtos.getTotal());
        result.setErrorMessage("操作成功");
        result.setHost("");
        return result;
    }

    /**
     * 保存敏感词
     * @param sensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(WmSensitive sensitive) {
        List<WmSensitive> list = this.list();
        for (WmSensitive wmSensitive : list) {
            if (wmSensitive.getSensitives().equals(sensitive.getSensitives())){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
        }

        sensitive.setCreatedTime(new Date());
        this.save(sensitive);
        return ResponseResult.okResult(200,"新增敏感词成功");
    }

    /**
     * 修改敏感词
     * @param sensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive sensitive) {
        this.updateById(sensitive);
        return ResponseResult.okResult(200,"编辑成功");
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        this.removeById(id);
        return ResponseResult.okResult(200,"删除成功");
    }
}
